package com.cola.service;

import com.cola.dao.GoodsPriceDao;
import com.cola.entity.export.ExportGoodsPrice;
import com.cola.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料价格管理Service
 * @author Max
 * @since 2017/9/8
 */
public class GoodsPriceService {
    private static volatile GoodsPriceService instance = null;
    private static final Logger logger = LoggerFactory.getLogger(GoodsPriceService.class);

    public static GoodsPriceService getInstance() {
        if (instance == null) {
            synchronized (GoodsPriceService.class) {
                if (instance == null) {
                    instance = new GoodsPriceService();
                }
            }
        }
        return instance;
    }
    /**
     * 检查商品价格有效日期时间
     * @param List<List<String>> list 导入的信息列表
     * @return Map<String,String> 检查返回结果集
     * @author Max
     * @since 2017/9/11
     */
    public Map<String,String> checkEffectiveDate(List<List<String>> list){
        Map<String,String> resultMap = new HashMap<String,String>();
        Map<String,String> goodsMaxDateMap = GoodsPriceDao.getInstance().getExistsGoodsPrice();
        String tempKey = null;
        for(List<String> tempList : list){
            tempKey = tempList.get(0)+tempList.get(1)+tempList.get(2);
            if(goodsMaxDateMap!=null){
                if(goodsMaxDateMap.containsKey(tempKey)){
                    if(DateUtils.dateDiff(goodsMaxDateMap.get(tempKey),tempList.get(4))!=1){
                        resultMap.put("result","error");
                        resultMap.put("msg","物料编号："+tempList.get(0)+"\n区域编号："+tempList.get(1)+"\n渠道编号："+tempList.get(2)+"\n有效起始时间与与当前配置冲突！");
                        return resultMap;
                    }
                }
            }
        }
        resultMap.put("result","success");
        return resultMap;
    }

    /**
     * 检查导入表格中有效期区间是否一致
     * @param List<List<String>> list
     * @return Map<String,String> 返回信息
     * @author Max
     * @since 2017/9/11
     */
    public Map<String,String> checkImportDate(List<List<String>> list){
        Map<String,String> resultMap = new HashMap<String,String>();
        Map<String,String> tempMap = new HashMap<String,String>();
        for(List<String> tempList : list){
            tempMap.put(tempList.get(4),"");
            tempMap.put(tempList.get(5),"");
        }
        if(tempMap.size()>2){
            resultMap.put("result","error");
            resultMap.put("msg","Excel表格中，有效期区间不一致");
        }else{
            resultMap.put("result","success");
        }
        return resultMap;
    }

    /**
     * 插入商品价格
     * @param List<List<String>> list 导入列表
     * @return Map<String,String>返回信息
     * @author Max
     * @since 2017/9/11
     */
    public boolean insertGoodsPrice(List<List<String>> list){
        return GoodsPriceDao.getInstance().insertGoodsPrice(list);
    }

    /**
     * 搜索商品价格列表
     * @param offset 起始位置
     * @param limit 页面大小
     * @param filter 搜索条件
     * @return List<Map<String,Object>>
     * @author Max
     * @since 2017/9/11
     */
    public List<Map<String,Object>> getGoodsPriceList(Integer offset,Integer limit,String filter){
        return GoodsPriceDao.getInstance().getGoodsPriceList( offset, limit, filter);
    }

    /**
     * 搜索商品价格数量
     * @param filter 搜索条件
     * @return Integer
     * @author Max
     * @since 2017/9/11
     */
    public Integer getGoodsPriceListCount(String filter){
        return GoodsPriceDao.getInstance().getGoodsPriceListCount(filter);
    }

    /**
     * 根据物料id获取价格配置信息
     * @param configId 配置Id
     * @return List<Map<String,Object>>
     * @author Max
     * @since 2017/9/11
     */
    public Map<String,Object> getGoodsPriceById(Integer configId){
        return GoodsPriceDao.getInstance().getGoodsPriceById(configId);
    }


    /**
     * 保存商品价格信息修改
     * @param configId
     * @param goodsPrice
     * @return true 成功 false 失败
     * @author Max
     * @since 2017/9/11
     */
    public boolean saveGoodsPriceConfig(Integer configId, BigDecimal goodsPrice){
        return GoodsPriceDao.getInstance().saveGoodsPriceConfig( configId,  goodsPrice);
    }


    /**
     * 停用价格配置
     * @param configId 价格配置Id
     * @return true 成功 false 失败
     * @author Max
     * @since 2017/9/11
     */
    public boolean doGoodsPriceOff(Integer configId){
        return GoodsPriceDao.getInstance().doGoodsPriceOff( configId);
    }

    /**
     * 获取价格导出数据
     * @return List<ExportGoodsPrice>
     * @author Max
     * @since 2017/9/11
     */
    public List<ExportGoodsPrice> getExportGoodsPriceInfo(){
        List<ExportGoodsPrice> list = GoodsPriceDao.getInstance().getExportGoodsPriceInfo();
        String nowDate= DateUtils.getCurrentDateTime();

        for(ExportGoodsPrice exportGoodsPrice : list){
            if(exportGoodsPrice.getCheck_status()==-1){
                exportGoodsPrice.setStatus("已停用");
            }else{
                if(exportGoodsPrice.getStart_time().compareTo(nowDate)>0){
                    exportGoodsPrice.setStatus("未生效");
                }else{
                    if(exportGoodsPrice.getEnd_time().compareTo(nowDate)>=0){
                        exportGoodsPrice.setStatus("生效中");
                    }else{
                        exportGoodsPrice.setStatus("已失效");
                    }
                }
            }
        }
        return list;
    }



    public static void main(String[] args) {
        System.out.println(DateUtils.dateDiff("2018-01-01","2018-01-02"));
    }


}
